package com.p2p.backend.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import jodd.util.StringUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.backend.dao.AccountDao;
import com.p2p.backend.dao.BackWithdrawDao;
import com.p2p.backend.model.BackendWithdraw;
import com.p2p.backend.model.WithdrawStatus;
import com.p2p.backend.service.BackWithdrawService;
import com.p2p.base.constents.InnerAccountConsts;
import com.p2p.base.model.Money;
import com.p2p.base.page.PageVo;
import com.p2p.front.enums.ChargeType;
import com.p2p.front.service.ChargeService;
import com.p2p.trans.client.enums.FreezeBalanceTypeEnum;
import com.p2p.trans.client.facade.AccountTransFacade;
import com.p2p.trans.client.model.TransInfo;
import com.p2p.trans.client.result.AccountTransResult;

/**
 * leiwen@yiqihao.com
 */
@Service("backWithdrawService")
public class BackWithdrawServiceImpl implements BackWithdrawService {

	@Autowired
	private BackWithdrawDao withdrawDao;

	@Autowired
	private AccountDao accountDao;

	@Resource
	private AccountTransFacade accountTrans;

	@Resource
	private ChargeService chargeService;

	@Override
	public PageVo listPage(PageVo form) {
		List<Map<String, Object>> list = withdrawDao.listPage(form);
		form.setList(list);
		return form;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void succeed(Integer id, String ip, Long uid) {
		// 修改status
		BackendWithdraw withdraw = new BackendWithdraw();
		withdraw.setId(id);
		withdraw.setUpip(ip);
		withdraw.setUptime(System.currentTimeMillis());
		withdraw.setStatus(WithdrawStatus.SUCCEED.getCode());
		withdrawDao.update(withdraw);
		
		String accountNo = accountDao.getAccountNo(uid);
		BackendWithdraw feeWithdraw=withdrawDao.get(id);
		/*Money fee=feeWithdraw.getFee();
		if(fee != null && fee.getCent() >0){
			chargeService.doChargeFromBalance(ChargeType.WITHDRAW_PROCEDURE_CHARGE, accountNo, uid, fee, ChargeType.WITHDRAW_PROCEDURE_CHARGE.getDefaultMemo());
		}*/
		BackendWithdraw withdrawInfo = withdrawDao.get(id);
		TransInfo transInfo = new TransInfo();
		transInfo.setTransAmount(withdrawInfo.getMoney());
		transInfo.setTransAccountOut(accountNo);
		transInfo.setFreezeType(FreezeBalanceTypeEnum.FREEZE_FOR_WITHDRAW);
		// 总账账号
		transInfo.setTransAccountIn(InnerAccountConsts.innerTotalAccountNo);
		AccountTransResult result = accountTrans
				.doUnFreezeAndWithdraw(transInfo);
		
		System.out.println(result);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void fail(Integer id, String ip, String remark, Long uid) {
		// 修改status，记下原因
		BackendWithdraw withdraw = new BackendWithdraw();
		withdraw.setId(id);
		withdraw.setUpip(ip);
		withdraw.setUptime(System.currentTimeMillis());
		withdraw.setRemark(remark);
		withdraw.setStatus(WithdrawStatus.FAIL.getCode());
		withdrawDao.update(withdraw);

		// 资金解冻
		String accountNo = accountDao.getAccountNo(uid);
		BackendWithdraw withdrawInfo = withdrawDao.get(id);
		TransInfo transInfo = new TransInfo();
		transInfo.setTransAmount(withdrawInfo.getMoney());
		transInfo.setTransAccountOut(accountNo);
		transInfo.setFreezeType(FreezeBalanceTypeEnum.FREEZE_FOR_WITHDRAW);
		AccountTransResult result = accountTrans.doUnFreeze(transInfo);
		System.out.println(result);
	}

	@Override
	public BackendWithdraw get(Integer id) {
		return withdrawDao.get(id);
	};

	@Override
	public Money sumForCondition(Map<String, Object> parames) {
		Long money = withdrawDao.sumForCondition(parames);
		if (money == null) {
			money = new Long(0);
		}
		return new Money(money);
	}
}
